/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2023-12-07 09:26:03
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-05-10 11:11:34
 * @FilePath: /yt-devops/lib/pages/common/select_page/select_page_controller.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:native_dart_package/http/request/exception/exception_handler.dart';
import 'package:native_dart_package/native_dart_package.dart';
import 'package:yt_devops/components/paging/model/paging_datas.dart';
import 'package:yt_devops/components/paging/model/paging_params.dart';
import 'package:yt_devops/components/paging/page_controller.dart';
import 'package:yt_devops/components/yt_form/props/text_select_props.dart';
import 'package:yt_devops/pages/common/select_page/select_page_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/services/task/task_services.dart';

class SelectPageController<T> extends PagingController<T, SelectPageModel<T>> {
  TextEditingController textController = TextEditingController();

  bool isLoad = false;

  String search = '';

  Timer? _timer;

  late SelectPageProp<T> props;

  @override
  void onInit() {
    props = Get.arguments;

    super.onInit();
  }

  @override
  void onReady() {}

  @override
  void onClose() {}

  List<String> get names =>
      pagingState.data.map((e) => props.getNameKey(e)).toList();

  List<String> get selectedNames =>
      props.initSelectValues.map((e) => props.getNameKey(e)).toList();

  /// 刷新列表
  refeshHandler(String text) {
    search = text;
    refreshData();
  }

  @override
  getState() {
    return SelectPageModel()..data.addAll(props.initSelectValues);
  }

  @override
  Future<PagingDatas<T>?> loadData(PagingParams pagingParams) async {
    if (!isLoad) {
      showLoading(toast: ResKey.loading.tr);
    }
    if (props.searchKey != null) {
      pagingParams.extra = {}..putIfAbsent(props.searchKey!, () => search);
    }

    try {
      var data = await props.service?.call(pagingParams);

      data = data;

      isLoad = true;

      Map<String, dynamic> pageMap = {};

      if (data is List) {
        pageMap = {
          'pageNum': 1,
          'pageSize': data.length,
          'total': data.length,
          'list': data
        };
      } else {
        pageMap = data;
      }

      PagingDatas<T> pagingData = PagingDatas.fromJson(pageMap);

      // 过滤掉以选择
      var list = pagingData.list ?? [];
      for (var selected in props.initSelectValues) {
        for (var i = 0; i < list.length; i++) {
          var item = list[i];
          if (props.getIdByKey(item) == props.getIdByKey(selected)) {
            list.removeAt(i);
            break;
          }
        }
      }

      if (pagingState.pageIndex == 1) {
        if (props.initSelectValues.isNotEmpty) {
          pagingData.list?.insert(0, props.initSelectValues[0]);
        }
      }
      return pagingData;
    } catch (e) {
      return PagingDatas<T>();
    } finally {
      dismissLoading();
    }
  }
}
